<?php
namespace app\admin\controller;

use think\Controller;
use think\Log;
use think\Exception;

class Article extends Base
{

    /**
     * 获取文章列表
     * @author wcy
     * @version 2017年5月5日15:27:11
     */
    public function articleList(){
        try{
            if($this->request->isAjax()||$this->request->isPost()){
                $data = $this->deDataTablesJson();
                $draw=$data['draw'];
                $start=$data["start"];
                $length=$data["length"];
                $search="";
                if(!empty($_POST["search"])){
                    $search=$_POST["search"];
                }
                $data=model("Article")->articleList($search,$start,$length);
                if(!$data){
                    return json(["draw"=>$draw,"recordsTotal"=>0,"recordsFiltered"=>0,"data"=>[]]);
                }else{
                    return json(["draw"=>$draw,"recordsTotal"=>$data["count"],"recordsFiltered"=>$data["count"],"data"=>$data["data"]]);
                }
            }else{
                return view("articlelist");
            }
        }catch (Exception $e){
            Log::record($e->getMessage());
            return false;
        }
    }
    /**
     * 添加文章
     * @author wcy
     * @version 2017年6月26日16:50:01
     */
    public function addArticle(){
        try{
            if($this->request->isAjax()||$this->request->isPost()){
                $data["title"]=$_POST["title"];
                $res=model("Article")->verifArticleTitle($data["title"]);
                if($res){
                    return json(["code"=>-1,"msg"=>"文章标题已经存在"]);
                }

                $data["content"]=$_POST["content"];
                $data["status"]=$_POST["status"];
                $data["dz_number"]=$_POST["dz_number"];
                $data["add_time"]=date("Y-m-d H:i:s");
                $data["type"]=$_POST["type"];
                $data["label"]=$_POST["label"];
                $data["is_yuan"]=$_POST["is_yuan"];
                $res=model("Article")->addArticle($data);
                if($res!==false){
                    return json(["code"=>1,"msg"=>"添加成功"]);
                }else{
                    return json(["code"=>-1,"msg"=>"添加失败"]);
                }
            }else{
                $data=model("Type")->getTypeAll();
                $labelData=model("Label")->labelList();
                $this->assign("labelData",$labelData);
                $this->assign("data",$data);
                return view("addarticle");
            }
        }catch (Exception $e){
            Log::record($e->getMessage());
            return view("addarticle");
        }
    }
    /**
     * 修改状态
     * @author wcy
     * @version 2017年6月27日10:01:34
     */
    public function editArticleStatus(){
        try{
            $id=$_POST["id"];
            $data["status"]=$_POST["status"];
            $res=model("Article")->editArticle($id,$data);
            if($res!==false){
                return json(["code"=>1,"msg"=>"修改成功"]);
            }else{
                return json(["code"=>-1,"msg"=>"修改失败"]);
            }
        }catch (Exception $e){
            Log::record($e->getMessage());
            return json(["code"=>-1,"msg"=>"修改失败"]);
        }
    }
    /**
     * 修改文章
     * @author wcy
     * @version 2017年6月26日16:58:58
     */
    public function editArticle(){
        try{
            if($this->request->isAjax()||$this->request->isPost()){
                $id=$_POST["id"];
                $data["title"]=$_POST["title"];
                $res=model("Article")->verifArticleTitleTwo($id,$data["title"]);
                if($res){
                    return json(["code"=>-1,"msg"=>"文章标题已经存在"]);
                }
                $data["content"]=$_POST["content"];
                $data["status"]=$_POST["status"];
                $data["type"]=$_POST["type"];
                $data["dz_number"]=$_POST["dz_number"];
                $data["label"]=$_POST["label"];
                $data["introduction"]=$_POST["introduction"];
                $data["is_yuan"]=$_POST["is_yuan"];
                $res=model("Article")->editArticle($id,$data);
                if($res!==false){
                    return json(["code"=>1,"msg"=>"修改成功"]);
                }else{
                    return json(["code"=>-1,"msg"=>"修改失败"]);
                }
            }else{
                $id=$_GET["id"];
                $typeData=model("Type")->getTypeAll();
                $this->assign("typeData",$typeData);
                $articleData=model("Article")->getArticle($id);
                $this->assign("articleData",$articleData);
                $labelData=model("Label")->labelList();
                $this->assign("labelData",$labelData);
                return view("editarticle");
            }
        }catch (Exception $e){
            Log::record($e->getMessage());
            return view("editarticle");
        }
    }
    /**
     * 删除文章
     * @author wcy
     * @version 2017年6月26日17:08:01
     */
    public function delArticle(){
        try{
            $id=$_POST["id"];
            $res=model("Article")->delArticle($id);
            if($res!==false){
                return json(["code"=>1,"msg"=>"删除成功"]);
            }else{
                return json(["code"=>-1,"msg"=>"删除失败"]);
            }
        }catch (Exception $e){
            Log::record($e->getMessage());
            return json(["code"=>-1,"msg"=>"删除失败"]);
        }
    }
}